package com.lottery.material.controller.base;

import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.util.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * @description 基础Controller
 * @author rambo
 * @date 2018年6月7日 下午6:16:54
 */
@SuppressWarnings("ALL")
public class BaseController extends BasicController {

	/**
	 * @description 得到PageData对象
	 * @return
	 */
	public PageData getPageData() {
		return new PageData(getRequest());
	}
	
	/**
	 * @description 得到ModelAndView对象
	 * @return
	 */
	public ModelAndView getModelAndView() {
		return new ModelAndView();
	}
	
	/**
	 * @description 得到request对象
	 * @return
	 */
	public HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * @description 得到32位的uuid
	 * @return
	 */
	public String get32UUID() {
		return StringUtil.getUUID();
	}
	
	/**
	 * @description 得到分页列表的信息
	 * @return
	 */
	public Page getPage() {
		return new Page();
	}
	
	/**
	 * @description 往session里面设置attribute
	 * @param key session的key
	 * @param value session的value
	 */
	public void setAttributeToSession(String key, Object value) {
		UserUtil.getSession().setAttribute(key, value);
	}
	
	/**
	 * @description 从session里面获取attribute
	 * @param key session的key
	 * @return 返回session的value
	 */
	public Object getAttributeFromSession(String key) {
		return UserUtil.getSession().getAttribute(key);
	}
	
	/**
	 * @description 在导出Excel之前，需要先判断是否进行了导出查询，
	 * 如果只是输入了查询条件但是没有去进行导出查询不允许导出Excel，控制导出Excel操作更加合理化
	 * @param pdKey 存储导出Excel的查询条件的pd的key
	 * @return 返回响应对象
	 */
	public ResponseResult isQueryData(String pdKey) {
		PageData pd = (PageData) getAttributeFromSession(pdKey);
		ResponseResult result = ResponseResult.buildResult(400, "请先查询出您需要导出的数据！");
		if (MapUtil.isNotBlank(getPageData())) {//如果查询条件至少有一个不为空
			if (!Objects.equals(pd, getPageData())) {
				return result;
			}
		} else {//如果所有的查询条件都为空
			if (AssertUtil.isNotEmpty(pd) && MapUtil.isNotBlank(pd)) {
				//setAttributeToSession(pdKey, new PageData()); TODO 以备后用
				return result;
			}
		}
		return ResponseResult.buildResult(200);
	}
	
	public static void logBefore(Logger logger, String interfaceName) {
		logger.info("");
		logger.info("start");
		logger.info(interfaceName);
	}
	
	public static void logAfter(Logger logger) {
		logger.info("end");
		logger.info("");
	}

	/**
	 * description: 公共请求参数非空验证
	 * author sunny
	 * createDate 2019-04-13 下午 1:39
	 * @param reqParamMap
	 * @return
	 */
	protected ResponseResult validateEmpty(Map<String, String> reqParamMap) {
		String appKey = reqParamMap.get("appKey");
		if (StringUtils.isBlank(appKey)) {
			return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "appKey");
		}
		String timestamp = reqParamMap.get("timestamp");
		if (StringUtils.isBlank(timestamp)) {
			return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "timestamp");
		}
		String sign = reqParamMap.get("sign");
		if (StringUtils.isBlank(sign)) {
			return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "sign");
		}
		String appVersion = reqParamMap.get("appVersion");
		if (StringUtils.isBlank(appVersion)) {
			return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "appVersion");
		}
		String deviceType = reqParamMap.get("deviceType");
		if (StringUtils.isBlank(deviceType)) {
			return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "deviceType");
		}
		return null;
	}


	protected ResponseResult validate(HttpServletRequest request, Map<String, String> reqParamMap){
		//解析参数
		if (ParameterUtil.analyticParameter(request, reqParamMap)) {
			throw new CustomException(ResultEnum.PARSE_EXCEPTION);
		}

		//非空判断
		ResponseResult respDate = validateEmpty(reqParamMap);
		if (null != respDate) {
			return respDate;
		}
		//签名验证
		ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
		if (null != responseResult) {
			return responseResult;
		}
		return null;
	}
}
